﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;

namespace Telecom.Service
{
    public class LoaiCapService
    {
        private readonly EntitiesTelecom dbContext = new EntitiesTelecom();

        public LoaiCapService()
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            dbContext.Configuration.ProxyCreationEnabled = false;
        }

        public LOAI_CAP GetById(string Id)
        {
            try
            {
                return dbContext.LOAI_CAP.Where(p => p.MA == Id).First();
            }
            catch (Exception e)
            {
                LOAI_CAP _listRe = new LOAI_CAP();
                return _listRe;
            }
        }

        public List<LOAI_CAP> getList()
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            try
            {
                return dbContext.LOAI_CAP.OrderByDescending(p => p.MA).ToList();
            }
            catch (Exception e)
            {
                List<LOAI_CAP> _listRe = new List<LOAI_CAP>();
                return _listRe;
            }
        }

        public List<_listJsontoTree> getListTree()
        {
            try
            {
                List<_listJsontoTree> _listRetuen = new List<_listJsontoTree>();


                _listJsontoTree itemMVT = new _listJsontoTree();
                itemMVT.id = "0";
                itemMVT.text = "-- Tất cả các loại cáp --";
                itemMVT.type = "ban";
                itemMVT.Idparent = "0";

                var _listMVT = dbContext.LOAI_CAP.ToList();

                foreach (var itemloaiTb in _listMVT)
                {
                    _listJsontoTree itemLoaiTb = new _listJsontoTree();
                    itemLoaiTb.id = itemloaiTb.MA.ToString();
                    itemLoaiTb.text = itemloaiTb.TEN;
                    itemLoaiTb.type = "lopcap";
                    itemLoaiTb.Idparent = "0";
                    itemMVT.children.Add(itemLoaiTb);
                }

                _listRetuen.Add(itemMVT);

                return _listRetuen;

            }
            catch (Exception e)
            {
                List<_listJsontoTree> _listRe = new List<_listJsontoTree>();
                return _listRe;
            }
        }

    }
}
